//
//  HMRetweetStatusView.swift
//  MyWeibo
//
//  Created by apple on 16/4/21.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
//想用约束类必须先导入头文件
import SnapKit


class HMRetweetStatusView: UIView {
    
    //设置当前控件的底部约束属性
    var bottomCons: Constraint?
    
    
    var statusViewModel: HMStatusViewModel? {
        didSet{
            // 设置转发微博内容
            contentLabel.text = statusViewModel?.retweetText
           
            
            //首先卸载控件的底部约束
            bottomCons?.uninstall()
            
            //- 配图视图的显示与隐藏
            //- 如果有配图
            if let pic_urls = statusViewModel?.status?.retweeted_status?.pic_urls where pic_urls.count > 0{
                
//                
//                //配图赋值
//                pictureView.pic_urls = pic_urls
//                
//                // 判断如果是1张图片，设置itemSize为配图视图的大小
//                if pic_urls.count == 1 {
//                    //拿到layout
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    //设置itemsize
//                    layout.itemSize = statusViewModel!.retweetPictureViewSize
//                    
//                }else{
//                    // 否则设置成正方形 HMStatusPictureItemWH
//                    let layout = pictureView.collectionViewLayout as! UICollectionViewFlowLayout
//                    layout.itemSize = CGSize(width: HMStatusPictureItemWH, height: HMStatusPictureItemWH)
//                }
                
                //- 1. 给配图视设置数据（以及大小宽高）
                
                pictureView.dataInfo = (pic_urls,statusViewModel!.retweetPictureViewSize)
                
//                pictureView.snp_updateConstraints(closure: { (make) -> Void in
//                    make.size.equalTo(statusViewModel!.retweetPictureViewSize)
//                    
//                })
                
                //- 2. 将配图显示出来
                pictureView.hidden = false
                // 3. 以转发微博视图为例：转发微博视图的底部等于配图视图的底部
                self.snp_updateConstraints(closure: { (make) -> Void in
                    bottomCons = make.bottom.equalTo(pictureView).offset(HMStatusCellMargin).constraint
                    
                })
                
            }else {
                // 如果没有配图
                // 1. 隐藏
                pictureView.hidden = true
                //2. 以转发微博视图为例：转发微博视图的底部等于文字label的底部
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                    bottomCons = make.bottom.equalTo(contentLabel).offset(HMStatusCellMargin).constraint
                })
            }
            
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        backgroundColor = UIColor(white: 242/255, alpha: 1)
        
        //        backgroundColor = UIColor.orangeColor()
        //添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        
        //转发内容约束
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.leading.top.equalTo(self).offset(HMStatusCellMargin)
        }
        
        //配图约束
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin)
            
            //调试暂时调整宽高
            make.size.equalTo(CGSizeMake(100, 100))
        }
        
        //自身约束
        self.snp_makeConstraints { (make) -> Void in
            //关联底部约束
            bottomCons = make.bottom.equalTo(contentLabel.snp_bottom).offset(HMStatusCellMargin).constraint
        }
    }
    
    // MARK: - 懒加载控件
    
    // 转发微博显示内容的lable
    private lazy var contentLabel: UILabel = UILabel(textColor: UIColor.darkGrayColor(), fontSize: HMStatusCellContentFontSize, preferredMaxLayoutWidth: SCREENW - 2 * HMStatusCellMargin)
    
    //懒加载配图  先给一个默认大小 和默认布局
    private lazy var pictureView:HMStatusPictureView = HMStatusPictureView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout())
}
